package com.group01.web.controller;

import com.group01.result.Result;
import com.group01.service.CommodityService;
import com.group01.vo.Commodity.CommodityGetInfoByIdVO;
import com.group01.vo.Commodity.CommodityInfoVO;
import com.group01.vo.Commodity.CommodityShowVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.DecimalMin;
import java.util.List;

/**
 * @Description
 * @Author 翟玉龙
 * @Date 2024/6/23
 */
@RestController
@RequestMapping("/book")
@Validated
@Api(tags = "商品模块")
public class CommodityController {

    /**
     * 注入商品服务
     */
    private final CommodityService commodityService;

    public CommodityController(CommodityService commodityService) {
        this.commodityService = commodityService;
    }


    /**
     * 首页展示图书信息（只展示20本）
     * @return {@link Result }<{@link List }<{@link CommodityShowVO }>>
     */
    @ApiOperation("首页展示图书信息")
    @GetMapping("/showBooks")
    public Result<List<CommodityShowVO>> showBooks() {

        return commodityService.showBooks();
    }

    /**
     * 首页获取分页所有图书信息
     * @param pageNum 页码
     * @param pageSize 页面大小
     * @return {@link Result }<{@link List }<{@link CommodityShowVO }>>
     */
    @ApiOperation("首页获取分页所有图书信息")
    @GetMapping("/{pageNum}/{pageSize}")
    public Result<List<CommodityShowVO>> getAllBooks(
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize
    ) {
        return commodityService.getAllBooks(pageNum,pageSize);
    }


    /**
     * 根据书名、作者、出版社检索图书信息
     * @param query 书名、作者、出版社
     * @return {@link Result }<{@link List }<{@link CommodityShowVO }>>
     */
    @ApiOperation("根据书名、作者、出版社检索图书信息")
    @GetMapping("/searchBook")
    public Result<List<CommodityShowVO>> searchBook(
            @RequestParam(value = "query",required = false) String query
    ) {
        return commodityService.searchBook(query);
    }

    /**
     * 根据分类检索图书信息
     * @param id 分类id
     * @return {@link Result }<{@link List }<{@link CommodityShowVO }>>
     */
    @ApiOperation("根据分类检索图书信息")
    @GetMapping("/findByCategory/{id}")
    public Result<List<CommodityInfoVO>> findByCategory(
            @PathVariable @DecimalMin(value = "1" , message = "id必须是数字且大于0") Integer id
    ) {
        return commodityService.findByCategory(id);
    }

    /**
     * 根据图书ID获取图书信息
     * @param id 图书ID
     * @return {@link Result }<{@link CommodityGetInfoByIdVO }>
     */
    @ApiOperation("根据图书ID获取图书信息")
    @GetMapping("/{id}")
    public Result<CommodityGetInfoByIdVO> getCommodityInfo(
            @PathVariable Integer id
    ) {
        return commodityService.getCommodityById(id);
    }
}
